<?php

namespace PolarisbackEndFrameRe\api\request\https;

/**
 * 接收HTTP请求
 * @author TINUOKAS1/Astoflow
 */
class ReceiveRequestForHTTPS {

    private $response;

    /**
     * 接受请求类的构造函数
     * @param string $url
     * @param string $mode
     * @param array $json
     * @param string|null $cafile 自定义证书文件路径
     * @return void
     */
    public function __construct(string $url, string $mode, array $json = [], ?string $cafile = null)
    {
        switch ($mode) {
            case "get":
                $this->response = $this->ReceiveGetRequest($url, $cafile);
                break;
            case "post":
                $this->response = $this->SendAndReceivePostRequest($url, $json, $cafile);
                break;
            case "put":
                $this->response = $this->SendAndReceivePutRequest($url, $json, $cafile);
                break;
            case "delete":
                $this->response = $this->SendAndReceiveDeleteRequest($url, $cafile);
                break;
            default:
                throw new \InvalidArgumentException("Unsupported request mode: $mode");
        }
    }

    /**
     * 发送POST请求并接收响应
     * @param string $url
     * @param array $data
     * @param string|null $cafile 自定义证书文件路径
     * @return array
     */
    public function SendAndReceivePostRequest(string $url, array $data, ?string $cafile = null): array
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 验证对等证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否存在一个公用名(common name)

        if ($cafile) {
            curl_setopt($ch, CURLOPT_CAINFO, $cafile);
        }

        $response = curl_exec($ch);

        if ($response === FALSE) {
            die('Error: ' . curl_error($ch));
        }

        curl_close($ch);

        // 处理响应数据
        $responseData = json_decode($response, true);

        if ($responseData !== null) {
            return $responseData;
        } else {
            echo "Invalid JSON data.";
            return [];
        }
    }

    /**
     * 发送PUT请求并接收响应
     * @param string $url
     * @param array $data
     * @param string|null $cafile 自定义证书文件路径
     * @return array
     */
    public function SendAndReceivePutRequest(string $url, array $data, ?string $cafile = null): array
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 验证对等证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否存在一个公用名(common name)

        if ($cafile) {
            curl_setopt($ch, CURLOPT_CAINFO, $cafile);
        }

        $response = curl_exec($ch);

        if ($response === FALSE) {
            die('Error: ' . curl_error($ch));
        }

        curl_close($ch);

        // 处理响应数据
        $responseData = json_decode($response, true);

        if ($responseData !== null) {
            return $responseData;
        } else {
            echo "Invalid JSON data.";
            return [];
        }
    }

    /**
     * 发送DELETE请求并接收响应
     * @param string $url
     * @param string|null $cafile 自定义证书文件路径
     * @return array
     */
    public function SendAndReceiveDeleteRequest(string $url, ?string $cafile = null): array
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 验证对等证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否存在一个公用名(common name)

        if ($cafile) {
            curl_setopt($ch, CURLOPT_CAINFO, $cafile);
        }

        $response = curl_exec($ch);

        if ($response === FALSE) {
            die('Error: ' . curl_error($ch));
        }

        curl_close($ch);

        // 处理响应数据
        $responseData = json_decode($response, true);

        if ($responseData !== null) {
            return $responseData;
        } else {
            echo "Invalid JSON data.";
            return [];
        }
    }

    /**
     * 接受GET请求
     * @param string $url
     * @param string|null $cafile 自定义证书文件路径
     * @return array
     */
    public function ReceiveGetRequest(string $url, ?string $cafile = null): array
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 验证对等证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否存在一个公用名(common name)

        if ($cafile) {
            curl_setopt($ch, CURLOPT_CAINFO, $cafile);
        }

        $response = curl_exec($ch);

        if ($response === FALSE) {
            die('Error: ' . curl_error($ch));
        }

        curl_close($ch);

        $data = json_decode($response, true);

        if ($data !== null) {
            return $data;
        } else {
            echo "Invalid JSON data.";
            return [];
        }
    }

    /**
     * 获取响应数据
     * @return array
     */
    public function getResponse(): array
    {
        return $this->response;
    }
}